//
//  XDMyOrgnizationController.m
//  XDEducation
//
//  Created by apple on 2017/4/28.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDMyOrgnizationController.h"
#import "XDOrganizationCell.h"
#import "XDOrganizationModel.h"
#import "XDExpericeModel.h"

@interface XDMyOrgnizationController ()

/** 数据源 */
@property(nonatomic,strong) NSMutableArray *dataModelArr;
@end

static NSString *cellId = @"OrganizationCell";

@implementation XDMyOrgnizationController

-(void)viewDidLoad{
    [super viewDidLoad];
    [self initTableView];
    
    [self initNetworkingWithRefreshStatus:RefreshStatePullDown];
 
}


-(void)initTableView{
    
    [self.tableView registerNib:[UINib nibWithNibName:@"XDOrganizationCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:cellId];
    self.tableView.estimatedRowHeight = 200;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

-(NSMutableArray *)dataModelArr{
    if (!_dataModelArr) {
        _dataModelArr = [NSMutableArray array];
    }
    return _dataModelArr;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataModelArr.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XDOrganizationCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (self.vcType == VCTypeOrganization) {     //判断cell类型   0表示辅导机构 1表示体验馆
        
        cell.orgModel = self.dataModelArr[indexPath.row];
        cell.cellType = CellTypeMyOrg;
    }else{
        
        cell.expericeModel = self.dataModelArr[indexPath.row];
        cell.cellType = CellTypeMyExperience;
    }
    //加入block
    WeakSelf(weakself)
    //取消加入
    cell.joinedBlock = ^(NSInteger dataId){
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定取消加入辅导机构?" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [weakself cancelJoinNetworkWithDataId:dataId index:indexPath.row];
        }]];
        
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        
        [self presentViewController:alert animated:YES completion:nil];
    
    };
    //设置默认
    cell.setDefaultBlock = ^(NSInteger dataId){
        [weakself setDefaultNetworkWithOrgId:dataId];
    };
    return cell;
}



//请求列表数据
-(void)initNetworkingWithRefreshStatus:(RefreshState)status{

    NSString *url;
    if (self.vcType == VCTypeOrganization) {
        url = BaseURL(@"organizationMember.do?action=myOrganizationList");
    }else{
        url = BaseURL(@"aflatunLesson.do?action=myAflatunList");
    }
    
    [MBProgressHUD showHUD];
    [XDHttpTool get:url authCode:[AccountTool shareAccountTool].currentAccount.authCode params:nil success:^(id responseObj) {
        [MBProgressHUD hideHUD];
        NSLog(@"%@",responseObj);
        NSArray *dictArr = [responseObj objectForKey:@"t"];
        if ([dictArr isKindOfClass:[NSNull class]]) {
            [MBProgressHUD showError:XDNoData];
        }else{
        
            NSMutableArray *tempArr = [NSMutableArray array];
            for (NSDictionary *dict in dictArr) {
                
                //判断数据源  是体验馆还是辅导机构
                if(_vcType == VCTypeOrganization){  //机构
                    XDOrganizationModel *model = [XDOrganizationModel modelWithDict:[dict objectForKey:@"organizationInfo"]];
                    [tempArr addObject:model];
                }else{  //体验馆
                    XDExpericeModel *model = [XDExpericeModel modelWithDict:[dict objectForKey:@"aflatunInfo"]];
                    [tempArr addObject:model];
                }
                
            }
            self.dataModelArr = tempArr;
            [self.tableView reloadData];
        }
        
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:XDNetWorkError];
        NSLog(@"%@",error);
    }];
}


/**
 取消加入

 @param dataId 机构id
 @param index 数据源index
 */
-(void)cancelJoinNetworkWithDataId:(NSInteger)dataId index:(NSInteger)index{

    NSString *url;
    NSDictionary *dict;
    if (self.vcType == VCTypeOrganization) {
        url = BaseURL(@"organizationMember.do?action=cancelJoinOrganization");
        
        dict = @{@"organizationId":@(dataId)};
        
    }else{
        
        url = BaseURL(@"aflatunLesson.do?action=setDefaultAflatun");
        
        dict = @{@"aflatunId":@(dataId)};
    }
    

    [MBProgressHUD showHUD];
    [XDHttpTool get:url authCode:[AccountTool shareAccountTool].currentAccount.authCode params:dict success:^(id responseObj) {
       [MBProgressHUD hideHUD];
        if ([[[responseObj objectForKey:@"code"] stringValue] isEqualToString:@"1000"]) {
            [MBProgressHUD showSuccess:@"取消成功"];
            [self.dataModelArr removeObjectAtIndex:index];
            [self.tableView reloadData];
        }else{
             [MBProgressHUD showError:@"取消失败"];
        }

        NSLog(@"%@",responseObj);
        
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:XDNetWorkError];
        NSLog(@"%@",error);
    }];
}


/**
 设置默认

 @param dataId 机构id
 */
-(void)setDefaultNetworkWithOrgId:(NSInteger)dataId{

    NSString *url;
    NSDictionary *dict;
    if (self.vcType == VCTypeOrganization) {
        dict = @{@"organizationId":@(dataId)};
        url = BaseURL(@"organizationMember.do?action=setDefaultOrganization");
        
    }else{
        dict = @{@"aflatunId":@(dataId)};
        url = BaseURL(@"aflatunLesson.do?action=setDefaultAflatun");
    }
    
    
    [MBProgressHUD showHUD];
    [XDHttpTool get:url authCode:[AccountTool shareAccountTool].currentAccount.authCode params:dict success:^(id responseObj) {
        [MBProgressHUD hideHUD];
        NSLog(@"%@",responseObj);
        if ([[[responseObj objectForKey:@"code"] stringValue] isEqualToString:@"1000"]) {
            [MBProgressHUD showSuccess:@"设置成功"];
            [self.tableView reloadData];
        }else{
            [MBProgressHUD showError:@"设置失败"];
        }
        
    } failure:^(NSError *error) {
        [MBProgressHUD showError:XDNetWorkError];
        [MBProgressHUD hideHUD];
        NSLog(@"%@",error);
    }];
}

@end
